<?php
require '../config.php';
require_once SESSION_INCLUDE;
require_once DB;
require_once USER;

session_start();

	$db = get_session_db($db_config);
	
	if(!user_is_logged($db))
	{
		echo "Utente non loggato.";
		$db->toString();
		exit();
	}
	
	$user = get_session_user();
	
	//modifica dell'utente
	$user->setDescription($_POST['user_description']);
	$user->set('user_username', $_POST['user_username']);
	$user->setEmail($_POST['user_email']);
	$user->set('user_msnm', $_POST['user_msnm']);
	$user->setAddress($_POST['user_address']);
	$user->setPhone($_POST['user_phone']);
	
	$old_pw = $_POST['user_password'];
	$new_pw = null;
	if(isset($_POST['new_password']))
	{
		if(strcmp($_POST['new_password'], $_POST['confirm_password']) == 0)
		{
			try{
				$user->setPassword($old_pw, $_POST['new_password']);	
			}
			catch (EntityException $e)
			{
				echo $e->getMessage();
			}
		}
	}
	
	$user->commit();
	
	$_SESSION['user'] = serialize($user);
	//header("Location:".$_SERVER['DOCUMENT_ROOT']."/admin.php");

?>